--9转序列下跌 变种，不是以close，而是开收盘中小的做比较值，并且最后一天的收盘价在60日均线上面
create or replace function getst9zd2(days int) returns setof record as
$$
declare
rec record;
begin
  for rec in EXECUTE '
	select b."date",b.code,b.macd, 
		s."name",s.industry from basic_data b  left join stocks s  on b.code=s.code 
	where 
	to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days||')
		and zd <2
		and s."timeToMarket" <>0 and to_date(to_char(s."timeToMarket",''99999999''),''YYYYMMDD'') < current_date-180
		--close
		and strpos(s."name",''ST'')=0
		and low > ma55
	  	and LEAST(open,close) < (select LEAST(open,close) from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+4||') and code=b.code )
	  	and (select LEAST(open,close) from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+1||') and code=b.code ) <  (select LEAST(open,close) from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+5||') and code=b.code )
	   	and (select LEAST(open,close) from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+2||') and code=b.code ) <  (select LEAST(open,close) from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+6||') and code=b.code )
	  	and (select LEAST(open,close) from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+3||') and code=b.code ) <  (select LEAST(open,close) from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+7||') and code=b.code )
		and (select LEAST(open,close) from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+4||') and code=b.code ) <  (select LEAST(open,close) from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+8||') and code=b.code )
		and (select LEAST(open,close) from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+5||') and code=b.code ) <  (select LEAST(open,close) from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+9||') and code=b.code )
		--and (select LEAST(open,close) from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+6||') and code=b.code ) <  (select LEAST(open,close) from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+10||') and code=b.code )
		--and (select LEAST(open,close) from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+7||') and code=b.code ) <  (select LEAST(open,close) from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+11||') and code=b.code )
		--and (select LEAST(open,close) from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+8||') and code=b.code ) <  (select LEAST(open,close) from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+12||') and code=b.code )
		--low
		--and  --(
		-- low <  (select low from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+2||') and code=b.code )
			--or (select low from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+1||') and code=b.code ) <  (select low from basic_data where to_date(date,''YYYY-MM-DD'') = getwd(current_date,'||days+3||') and code=b.code )
			--)
	order by macd::float8;
	' loop
    return next rec;
  end loop;
return;
end
$$
language 'plpgsql';